package com.bt.lms.ui.page.sys.major.action;

import com.alibaba.fastjson.JSONObject;
import com.bt.lms.constants.BOConstant;
import com.bt.lms.constants.SysBizTypeConstant;
import com.bt.lms.entity.Major;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.ui.page.sys.utils.ArrayUtils;
import com.bt.lms.ui.page.sys.utils.Encode;
import com.bt.lms.ui.page.sys.ServerInfo;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.ui.page.sys.major.PageDetails;
import com.bt.lms.ui.page.sys.utils.NetWorkUtils;
import com.bt.lms.ui.page.sys.vo.MajorTableVo;
import com.bt.lms.ui.vo.PageSkipInfo;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.layout.Pane;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

/**
 * 专业列表界面的查看详情按钮事件
 */
public class MLDetailsBtnAction implements EventHandler<ActionEvent> {

    /**
     * 要查看用户的专业编号
     */
    private final MajorTableVo majorTableVo;

    /**
     * 界面跳转信息
     */
    private final PageSkipInfo pageSkipInfo;

    /**
     * 构建事件
     *
     * @param majorTableVo 要查看详情的专业列表信息
     * @param pageSkipInfo 界面跳转数据
     */
    public MLDetailsBtnAction(MajorTableVo majorTableVo, PageSkipInfo pageSkipInfo) {
        this.majorTableVo = majorTableVo;
        this.pageSkipInfo = pageSkipInfo;
    }

    /**
     * 事件实现
     *
     * @param event the event which occurred
     */
    @Override
    public void handle(ActionEvent event) {

        //1.组装网络消息包
        ReqMsg reqMsg = Encode.encodeToMsg(majorTableVo, SysBizTypeConstant.MAJOR_DETAILS);
        byte[] bytes = reqMsg.toBytes();
        //2.组网
        Socket socket = packageNet();
        OutputStream out;
        try {
            out = socket.getOutputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //3.发送
        boolean b = sendMesNet(out, bytes);
        //判断是否发送成功
        if (b) {
            //准备接收响应
            try {
                InputStream in = socket.getInputStream();
                //接收响应
                List<byte[]> bufS = NetWorkUtils.receive(in);
                //处理结果
                byte[] respBytes = ArrayUtils.joinBytes(bufS);
                //转消息
                RespMsg respMsg = RespMsg.toRespMsg(respBytes);
                //比较状态
                short status = respMsg.getStatus();
                if (status == RespMsg.NETWORK_STATUS_NORMAL) {
                    //成功，处理结果集
                    String data = respMsg.getData();
                    Major major = JSONObject.parseObject(data, Major.class);
                    finish(major);
                }
                //关闭资源
                in.close();
                out.close();
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }


        } else
            fail();
    }

    /**
     * 组网
     *
     * @return 网
     */
    private Socket packageNet() {
        String ip = ServerInfo.IP;
        Integer port = ServerInfo.PORT;

        try {
            return new Socket(ip, port);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 发送网络消息包
     *
     * @param out   输出流
     * @param bytes 要发送的byte数组
     * @return 发送结果
     */
    private boolean sendMesNet(OutputStream out, byte[] bytes) {

        //发送网络
        try {
            out.write(bytes);
            out.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;

    }

    /**
     * 失败提示
     */
    private void fail() {

        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("有误");
        alert.setHeaderText(null);
        alert.setContentText("网络异常，请检查网络或联系客服。");
        alert.showAndWait();

    }

    /**
     * 程序结束
     * @param major 专业信息
     */
    private void finish(Major major) {

        //跳转界面需要的数据
        Pane pane = pageSkipInfo.getPane();
        Integer index = pageSkipInfo.getIndex();

        //要跳转到的界面
        PageDetails pageDetails = new PageDetails(major, pageSkipInfo);
        //界面跳转
        ObservableList<Node> children = pane.getChildren();
        children.set(index, pageDetails);
    }

}
